package com.DongBing.service.impl;

import com.DongBing.common.exception.DongBingException;
import com.DongBing.common.utils.Res;
import com.DongBing.dao.AttrAttrgroupRelationDao;
import com.DongBing.dao.AttrGroupDao;
import com.DongBing.dao.CategoryDao;
import com.DongBing.entity.AttrAttrgroupRelationEntity;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.val;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.DongBing.common.utils.PageUtils;
import com.DongBing.common.utils.Query;

import com.DongBing.dao.AttrDao;
import com.DongBing.entity.AttrEntity;
import com.DongBing.service.AttrService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;


@Service("attrService")
@RequiredArgsConstructor
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    private final AttrAttrgroupRelationDao dao;

    private final CategoryDao categoryDao;

    private final AttrGroupDao attrGroupDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                new QueryWrapper<AttrEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void insert(AttrEntity attr) {
        val x = baseMapper.insert(attr);
        val entity = new AttrAttrgroupRelationEntity();
        entity.setAttrGroupId(attr.getAttrGroupId());
        entity.setAttrId(attr.getAttrId());
        val y = dao.insert(entity);
        if (x!=1||y!=1) {
            throw new DongBingException(404,"系统异常");
        }
    }

    @Override
    public Res<?> AttrListPage(Map<String, Object> map, String catelogId) {
        val wrapper = new LambdaQueryWrapper<AttrEntity>();
        wrapper.eq(AttrEntity::getCatelogId,catelogId);
        val key = (String) map.get("key");
        wrapper.and(!ObjectUtils.isEmpty(key),(obj)->{
           obj.like(AttrEntity::getAttrName,key);
           obj.or().eq(AttrEntity::getAttrId,key);
        });
        val page = this.page(
                new Query<AttrEntity>().getPage(map),
                wrapper
        );
         page.getRecords().forEach(e->{
            val categoryEntity = categoryDao.selectById(e.getCatelogId());
            e.setName(categoryEntity.getName());
            val attrAttrgroupRelationEntity =
                    dao.selectOne(new LambdaQueryWrapper<AttrAttrgroupRelationEntity>().eq(AttrAttrgroupRelationEntity::getAttrId,e.getAttrId()));
            val attrGroupEntity = attrGroupDao.selectById(attrAttrgroupRelationEntity.getAttrGroupId());
            e.setAttrGroupName(attrGroupEntity.getAttrGroupName());
        });
        return Res.success("成功",page);
    }

}
